package org.streets.eis;

import org.apache.wicket.Page;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebRequest;
import org.streets.database.SQLConnectionManager;
import org.streets.wicket.HiveRequestCycle;
import org.streets.wicket.HiveWebApplication;

public class EisRequestCycle extends HiveRequestCycle {

	private final SQLConnectionManager _manager;
	public EisRequestCycle(HiveWebApplication application, WebRequest request,
			Response response) {
		
		super(application, request, response);
		_manager = registry.service(SQLConnectionManager.class);
	}
	
	@Override
	protected void onBeginRequest() {
		super.onBeginRequest();
		if (!(getRequest().getPath().startsWith("resources/"))) {
		    _manager.begin();
		}
	}
	
	@Override
	protected void onEndRequest() {
	    if (!(getRequest().getPath().startsWith("resources/"))) {
	        _manager.commit();
	    }
		super.onEndRequest();		
	}
	
    @Override         
    public Page onRuntimeException(Page page, RuntimeException e) {
        if (!(getRequest().getPath().startsWith("resources/"))) {
            _manager.abort();    	
        }
    	return super.onRuntimeException(page, e);         
    }
}
